var xmlHttp;
var mpmap;
var mpmapContainer;
var mpmapContainerId       = "main-map";
var sidebarContainerId     = "sidebar-left";
var mpListId               = "mplace-list";
var getMpListUrl           = "/mplace/glist";
var initLatCenter          = 37.597126;
var initLngCenter          = -122.082926;
var initZoom               = 10;
var additionalHeightOffset = 10;
var deselectCurrent        = function() {};


window.onresize = handleResize;
window.onload   = handleLoad;


function handleLoad()
{
  if (GBrowserIsCompatible())
  {
    mpmapContainer = document.getElementById(mpmapContainerId);
    handleResize();
    mpmapInit();
    getMpList();
  }
}; /* mpmapInit() */


function handleResize()
{
  var offsetTopMap = 0;
  var mapHeight;
  var winHeight;
  var elem;

  for (elem = mpmapContainer; elem != null; elem = elem.offsetParent)
  {
    offsetTopMap = offsetTopMap + elem.offsetTop;
  }

  winHeight = getWindowHeight();
  mapHeight = winHeight - offsetTopMap - additionalHeightOffset;

  if (mapHeight > 0)
  {
    mpmapContainer.style.height = mapHeight + "px";
    document.getElementById(sidebarContainerId).style.height = mapHeight + "px";
  }
}; /* handleResize() */


function getWindowHeight()
{
  /*
   * Standard browsers (Mozilla, Safari, etc.)
   */
  if (self.innerHeight)
  {
    return self.innerHeight;
  }

  /*
   * IE 6
   */
  if (document.documentElement && document.documentElement.clientHeight)
  {
    return document.documentElement.clientHeight;
  }

  /*
   * IE 5
   */
  if (document.body)
  {
    return document.body.clientHeight;
  }

  /*
   * Just in case.
   */
  return 0;
}; /* getWindowHeight() */


function mpmapInit() 
{
  mpmap = new GMap2(mpmapContainer, {draggableCursor:"crosshair"});

  mpmap.addControl(new GLargeMapControl());
  mpmap.addControl(new GMapTypeControl());
  mpmap.addControl(new GOverviewMapControl());
  mpmap.setCenter(new GLatLng(initLatCenter, initLngCenter), initZoom);
}; /* mpmapInit() */


function getMpList()
{ 
  xmlHttp = GXmlHttp.create();
  xmlHttp.open("GET", getMpListUrl, true);
  xmlHttp.onreadystatechange = glistRespHandler;
  xmlHttp.send(null);
}; /* getMpList() */


function glistRespHandler()
{
  if (xmlHttp.readyState == 4)
  {
    if (xmlHttp.status != 200)
    {
      setLoadingMsgText('Could not access map data.');
      return;
    }

    /*
     * Decode JSON stream data into a Javascript array.
     */
    glistRespArray = eval('(' + xmlHttp.responseText + ')');

    if (!glistRespArray)
    {
      setLoadingMsgText('Map data error.');
      return;
    }

    for (i in glistRespArray)
    {
      processOneMpRecord(glistRespArray[i]);
    }

    changeBodyClass('loading', 'loaded');
  }
}; /* glistRespHandler() */


function setLoadingMsgText(str)
{
  document.getElementById('loading-msg-bar').innerHTML = '<p>' + str + '</p>';
}; /* setLoadingMsgText() */


function changeBodyClass(from, to)
{
  document.body.className = document.body.className.replace(from, to);
  return false;
}; /* changeBodyClass() */


function processOneMpRecord(mp)
{
  var gPoint;
  var gMarker;
  var htmlStr;
  var gInfoTabs;

  var listItem;
  var listItemLink;
  var focusPoint;

  gPoint                 = new GLatLng(parseFloat(mp[8]), parseFloat(mp[9]));
  gMarker                = new GMarker(gPoint);
  htmlStr                = genGInfoHtmlStr(mp);
  gInfoTabs              = [new GInfoWindowTab(mp[0], htmlStr)];

  listItem               = document.createElement('li');
  listItemLink           = listItem.appendChild(document.createElement('a'));
  listItemLink.href      = "#";
  listItemLink.innerHTML = genMpListDisplayStr(mp);

  focusPoint = function()
  {
    deselectCurrent();
    listItem.className = 'current';
    deselectCurrent = function()
    {
      listItem.className = '';
    }
    gMarker.openInfoWindowTabsHtml(gInfoTabs);
    mpmap.panTo(gPoint);
    return false;
  }

  GEvent.addListener(gMarker, 'click', focusPoint);
  listItemLink.onclick = focusPoint;
  document.getElementById(mpListId).appendChild(listItem);
  mpmap.addOverlay(gMarker);
}; /* processOneMpRecord() */


function genMpListDisplayStr(mp)
{
  return '<strong>' + mp[1] + '</strong><span>' +  mp[4] + ', ' + mp[5] + '</span>';
}; /* genMpListDisplayStr() */


function genGInfoHtmlStr(mp)
{
  var htmlStr;

  /*
   * [0]  => 'nid'
   * [1]  => 'title'
   * [2]  => 'street'
   * [3]  => 'suite'
   * [4]  => 'city'
   * [5]  => 'state'
   * [6]  => 'zip'
   * [7]  => 'country'
   * [8]  => 'lat'
   * [9]  => 'lng'
   * [10] => 'phone'
   */

  htmlStr = '<div class="gmap_info"><ul style="list-style-type: none; margin: 0; padding: 0">';
  htmlStr = htmlStr + '<li><a href="/node/' + mp[0] + '">' + mp[1] + '</a></li>';
  htmlStr = htmlStr + '<li>' + mp[2];
  if (mp[3] != '') htmlStr = htmlStr + ' ' + mp[3] + '</li>';
  else htmlStr = htmlStr + '</li>';
  htmlStr = htmlStr + '<li>' + mp[4] + ', ' + mp[5];
  if (mp[6] != '') htmlStr = htmlStr + ' ' + mp[6] + ', ' + mp[7] + '</li>';
  else htmlStr = htmlStr + ', ' + mp[7] + '</li>';
  htmlStr = htmlStr + '<li>' + mp[10] + '</li>';
  htmlStr = htmlStr + '</ul></div>';

  return htmlStr;
}; /* genGInfoHtmlStr() */
